home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / CCDBMS.ASC < prev    next >
Text File  |  1997-06-20  |  3KB  |  127 lines

  1. _Concurrent Database Commands and C++_
  2. by Harold R. Kasperink and John C. Dekker
  3.  
  4.  
  5. Listing One
  6. class CThread;
  7. void ThreadFunc(CThread *);
  8.  
  9. class CThread 
  10. {
  11. public:
  12.         CThread();
  13.         virtual ~CThread();
  14.  
  15.         void    Create();
  16.         void    Cancel();
  17.         void    Detach();
  18.         void    Join();
  19.  
  20.         friend  void    ThreadFunc(CThread *);
  21.         virtual void    Process(void) = 0;
  22. private:
  23.         pthread_t       m_Thread;
  24. };
  25.  
  26.  
  27. Listing Two
  28. class CMutex 
  29. {
  30. public:
  31.         CMutex();
  32.         ~CMutex();
  33.         void    Lock();
  34.         void    Unlock();
  35.         int     TryLock();
  36. private:
  37.         pthread_mutex_t         m_Mutex;
  38. };
  39.  
  40.  
  41. Listing Three
  42. #define MAX_NR_DBASES   15
  43.  
  44. class CArrayDbase 
  45. {
  46. private:
  47.     // Pointer to global database connection
  48.     static CArrayDbase      *g_pDbArray;                    
  49.     // Mutex to make sure only one reserve/release at a time
  50.     static CMutex           g_mtxArray;                 
  51.     // Array of database connections
  52.     CDbase*             m_pDbases[MAX_NR_DBASES];   
  53.     // Number of database connections
  54.     int             m_nNrDbases;
  55. public:
  56.     CArrayDbase(int nNrConnections, const char *szUsr, 
  57.                                    const char *szPasswd, const char *szDB);
  58.     virtual ~CArrayDbase();
  59.     // Get pointer to free database connection and reserve database connnection
  60.     static  CDbase*     ReserveDbase();
  61.     // Release database connection
  62.     static  void        ReleaseDbase(CDbase &dbase);
  63. private:
  64.     static void         Lock();
  65.     static void         Unlock();
  66. };
  67.  
  68.  
  69. Listing Four
  70. {
  71. private:
  72.     friend class    CArrayDbase;
  73.  
  74.     // Locks usage of object
  75.     CMutex      m_mtxInuse; 
  76.     // Instruct dbase-thread to do command
  77.     CMutex      m_mtxStartSql;  
  78.     // Informs instruct-thread command is done
  79.     CMutex      m_mtxEndSql;    
  80.     // Make sure no parallel usage of do 
  81.     CMutex      m_mtxDoCmd;     
  82.     // boolean flag indicates if connected
  83.     boolean         m_bConnected;   
  84.     // Continue flag
  85.     boolean         m_bContinue;    
  86.     // Current command
  87.     CDbCommand*     m_pCommand;     
  88.     // Return code of command execution
  89.     long            m_lSql;         
  90. public:
  91.     CDbase();
  92.     virtual ~CDbase();
  93.     // Command functions
  94.     long            Do(CDbCommand &command);
  95. private:
  96.     // Thread loop overwrite
  97.     virtual void    Process();
  98.     // Inuse Funtions
  99.     boolean         TryLock();
  100.     void            Lock();
  101.     void            Unlock();
  102. };
  103.  
  104. Listing Five
  105. class CDbCommand
  106. {
  107. private:
  108.     friend class CDbase;
  109.     CDbase *    m_pDbase;
  110. protected:
  111.     long        m_lSql;
  112. public:
  113.     CDbCommand();
  114.     CDbCommand(CDbase &dbase);
  115.     virtual ~CDbCommand();
  116.  
  117.     virtual void    Do() = 0;
  118.  
  119.     // Get and set functions
  120.    CDbase *        Dbase();
  121.     void            Dbase(CDbase &dbase);
  122. private:
  123.     // Is called by database object to instruct command to go
  124.     virtual long    Execute() = 0;
  125. };
  126.  
  127.